package com.mnc.web.action.manage;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mnc.entity.Article;
import com.mnc.entity.ArticleComment;
import com.mnc.entity.Notice;
import com.mnc.entity.PageNote;
import com.mnc.entity.UnitTalk;
import com.mnc.entity.User;
import com.mnc.entity.UserPage;
import com.mnc.service.inte.INoticeService;
import com.mnc.util.Helper;
import com.mnc.util.PageUtil;
import com.mnc.util.ServiceUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class CheckAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8035561005675569150L;
	
	private @Resource ServiceUtil serviceUtil;
	private @Resource PageUtil pageUtil;
	private int curPage=1;
	private String returnMsg;
	private boolean view;
	private int tempId;
	
	@JSON(serialize=false)
	public ServiceUtil getServiceUtil() {
		return serviceUtil;
	}
	public void setServiceUtil(ServiceUtil serviceUtil) {
		this.serviceUtil = serviceUtil;
	}
	@JSON(serialize=false)
	public PageUtil getPageUtil() {
		return pageUtil;
	}
	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}
	@JSON(serialize=false)
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	@JSON(serialize=false)
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public String getReturnMsg() {
		return returnMsg;
	}
	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}
	@JSON(serialize=false)
	public boolean isView() {
		return view;
	}
	public void setView(boolean view) {
		this.view = view;
	}
	@JSON(serialize=false)
	public int getTempId() {
		return tempId;
	}
	public void setTempId(int tempId) {
		this.tempId = tempId;
	}
	//显示举报信
	public String showAccuses() {
		int count = (int)this.getServiceUtil().getNoticeService().getCount(Notice.class, Helper.WHERE_GET_NEW_ACCUSE, new Object[] {false});
		int start=(curPage-1)*Helper.PAGE_SIZE;
		List<Notice> data = this.getServiceUtil().getNoticeService().getPageData(Notice.class, Helper.WHERE_GET_NEW_ACCUSE, new Object[] {false},start,Helper.PAGE_SIZE);
		this.getPageUtil().buildPage(count, "check_showAN.action?curPage=", this.getCurPage());
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("page", this.getPageUtil());
		return "ok";
	}
	//审核举报信
	public String checkAccuses() {
		if (tempId>0) {
			INoticeService noticeService = this.serviceUtil.getNoticeService();
			Notice find = noticeService.find(Notice.class, tempId);
			String nc=find.getNoticeContent();
			find.setNoticeRead(true);
			noticeService.update(find);
			int usrId = Integer.parseInt(nc.substring(nc.indexOf("usrId")+6));
			if (!view) {
				Notice notice=new Notice();
				User user=new User();
				user.setUserId(usrId);
				notice.setReciver(user);
				notice.setNoticeDate(new Date());
				notice.setNoticeContent("forchk_"+nc.substring(0,nc.indexOf("&usrId")));
				noticeService.add(notice);
			}
			this.setReturnMsg("操作成功！");
		}else {
			this.setReturnMsg("服务器未响应,请稍后再试！");
		}
		return "ok";
	}
	//显示用户空间留言
	public String showPageNotes() {
		int count = (int)this.getServiceUtil().getUserPageService().getCount(PageNote.class, Helper.WHERE_GET_PNOTE, new Object[]{true});
		int start=(curPage-1)*Helper.PAGE_SIZE;
		List<PageNote> data = this.getServiceUtil().getUserPageService().getPageData(PageNote.class, Helper.WHERE_GET_PNOTE, new Object[]{true},start,Helper.PAGE_SIZE);
		this.getPageUtil().buildPage(count, "check_showPN.action?curPage=", this.getCurPage());
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("page", this.getPageUtil());
		return "ok";
	}
	//审核留言
	public String checkPageNote() {
		if (tempId>0) {
			PageNote find = this.getServiceUtil().getPageNoteService().find(PageNote.class, tempId);
			find.setNoteView(view);
			this.getServiceUtil().getPageNoteService().update(find);
			this.setReturnMsg("操作成功！");
		}else {
			this.setReturnMsg("服务器未响应,请稍后再试！");
		}
		return "ok";
	}
//	显示文章评论
	public String showArticleComments() {
		int count = (int)this.getServiceUtil().getArticleCommentService().getCount(ArticleComment.class, Helper.WHERE_GET_ACOMMENT, new Object[]{true});
		int start=(curPage-1)*Helper.PAGE_SIZE;
		List<ArticleComment> data = this.getServiceUtil().getArticleCommentService().getPageData(ArticleComment.class, Helper.WHERE_GET_ACOMMENT, new Object[]{true},start,Helper.PAGE_SIZE);
		this.getPageUtil().buildPage(count, "check_showAC.action?curPage=", this.getCurPage());
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("page", this.getPageUtil());
		return "ok";
	}
	//审核文章评论
	public String checkArticleCommont() {
		if (tempId>0) {
			ArticleComment find = this.getServiceUtil().getArticleCommentService().find(ArticleComment.class, tempId);
			find.setCommentView(view);
			this.getServiceUtil().getArticleCommentService().update(find);
			this.setReturnMsg("操作成功！");
		}else {
			this.setReturnMsg("服务器未响应,请稍后再试！");
		}
		return "ok";
	}
//	显示空间
	public String showPages() {
		int count = (int)this.getServiceUtil().getUserPageService().getCount(UserPage.class, Helper.WHERE_GET_UPAGE, new Object[]{true});
		int start=(curPage-1)*Helper.PAGE_SIZE;
		List<UserPage> data = this.getServiceUtil().getUserPageService().getPageData(UserPage.class, Helper.WHERE_GET_UPAGE, new Object[]{true},start,Helper.PAGE_SIZE);
		this.getPageUtil().buildPage(count, "check_showUP.action?curPage=", this.getCurPage());
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("page", this.getPageUtil());
		return "ok";
	}
	//审核空间
	public String checkUserPage() {
		if (tempId>0) {
			UserPage find = this.getServiceUtil().getUserPageService().find(UserPage.class, tempId);
			find.setPageView(view);
			this.getServiceUtil().getUserPageService().update(find);
			if (!view) {
				Notice notice=new Notice();
				notice.setReciver(find.getOwner());
				notice.setNoticeDate(new Date());
				notice.setNoticeContent("forchk_detailUP.action?tempId="+tempId);
				this.serviceUtil.getNoticeService().add(notice);
			}
			this.setReturnMsg("操作成功！");
		}else {
			this.setReturnMsg("服务器未响应,请稍后再试！");
		}
		return "ok";
	}
	//空间详细
	public String detailUserPage() {
		if (tempId>0) {
			UserPage find = this.getServiceUtil().getUserPageService().find(UserPage.class, tempId);
			this.setReturnMsg(find.getPageShow());
		}
		return "ok";
	}
//	显示文章
	public String showArticles() {
		int count = (int)this.getServiceUtil().getArticleService().getCount(Article.class, Helper.WHERE_GET_UARTICLE, new Object[]{true});
		int start=(curPage-1)*Helper.PAGE_SIZE;
		List<Article> data = this.getServiceUtil().getArticleService().getPageData(Article.class, Helper.WHERE_GET_UARTICLE, new Object[]{true},start,Helper.PAGE_SIZE);
		this.getPageUtil().buildPage(count, "check_showUA.action?curPage=", this.getCurPage());
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("page", this.getPageUtil());
		return "ok";
	}
	//审核文章
	public String checkArticle() {
		if (tempId>0) {
			Article find = this.getServiceUtil().getArticleService().find(Article.class, tempId);
			find.setArticleView(view);			
			this.getServiceUtil().getArticleService().update(find);
			if (!view) {
				Notice notice=new Notice();
				notice.setReciver(find.getWriter());
				notice.setNoticeDate(new Date());
				notice.setNoticeContent("forchk_detailUA.action?tempId="+tempId);
				this.serviceUtil.getNoticeService().add(notice);
			}
			this.setReturnMsg("操作成功！");
		}else {
			this.setReturnMsg("服务器未响应,请稍后再试！");
		}
		return "ok";
	}
	//文章详细
	public String detailArticle() {
		if (tempId>0) {
			Article find = this.getServiceUtil().getArticleService().find(Article.class, tempId);
			this.setReturnMsg(find.getArticleContent());
		}
		return "ok";
	}
//	显示团讨论
	public String showUnitTalks() {
		int count = (int)this.getServiceUtil().getArticleService().getCount(UnitTalk.class, Helper.WHERE_GET_UTALK, new Object[]{true});
		int start=(curPage-1)*Helper.PAGE_SIZE;
		List<UnitTalk> data = this.getServiceUtil().getArticleService().getPageData(UnitTalk.class, Helper.WHERE_GET_UTALK, new Object[]{true},start,Helper.PAGE_SIZE);
		this.getPageUtil().buildPage(count, "check_showUT.action?curPage=", this.getCurPage());
		ActionContext.getContext().put("data", data);
		ActionContext.getContext().put("page", this.getPageUtil());
		return "ok";
	}
	//审核发言
	public String checkUnitTalk() {
		if (tempId>0) {
			UnitTalk find = this.getServiceUtil().getUnitTalkService().find(UnitTalk.class, tempId);
			find.setTalkView(view);
			this.getServiceUtil().getUnitTalkService().update(find);
			if (!view) {
				Notice notice=new Notice();
				notice.setReciver(find.getTalker());
				notice.setNoticeDate(new Date());
				notice.setNoticeContent("forchk_detailUT.action?tempId="+tempId);
				this.serviceUtil.getNoticeService().add(notice);
			}
			this.setReturnMsg("操作成功！");
		}else {
			this.setReturnMsg("服务器未响应,请稍后再试！");
		}
		return "ok";
	}
	//发言详细
	public String detailUnitTalk() {
		if (tempId>0) {
			UnitTalk find = this.getServiceUtil().getUnitTalkService().find(UnitTalk.class, tempId);
			this.setReturnMsg(find.getTalkContent());
		}
		return "ok";
	}
}
